<template>
	<view class="user-layout page-bg" v-if="userinfo">
		<view :style="{height: getNavBarHeight() + 'px'}"></view>
		<view class="user-info">
			<view class="avatar">
				<image src="/static/xxmLogo.png" mode="aspectFill"></image>
			</view>
			<view class="ip">{{userinfo.IP}}</view>
			<view class="address">来自于{{userinfo.address.city || userinfo.address.province || userinfo.address.country}}
			</view>
		</view>

		<view class="section">
			<view class="list">
				<view class="row" v-for="(item, index) in funcItems" :key="index" :url="item.url"
					@click="itemOnTap(item)">
					<view class="left">
						<uni-icons :type="item.type" size="20" color="#28b389"></uni-icons>
						<view class="text">{{item.name}}</view>
					</view>
					<view class="right">
						<view class="text">{{item.count}}</view>
						<uni-icons type="right" size="16" color="#aaa"></uni-icons>
					</view>

					<!-- #ifdef MP -->
					<button v-if="item.name=='联系客服'" open-type="contact">联系客服</button>
					<!-- #endif -->

					<!-- #ifndef MP -->
					<button v-if="item.name=='联系客服'" @click="call">拨打电话</button>
					<!-- #endif -->
				</view>

			</view>
		</view>

		<view class="section">
			<view class="list">
				<view class="row" v-for="(item, index) in otherItems" :key="index" @click="itemOnTap(item)">
					<view class="left">
						<uni-icons :type="item.type" size="20" color="#28b389"></uni-icons>
						<view class="text">{{item.name}}</view>
					</view>
					<view class="right">
						<view class="text">{{item.count}}</view>
						<uni-icons type="right" size="16" color="#aaa"></uni-icons>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue';
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		getNavBarHeight
	} from '@/utils/system.js'
	import {
		apiUserInfo
	} from '@/api/api.js'

	const userinfo = ref(null)

	onLoad(() => {

		getUserInfo()
	})

	const funcItems = ref([{
			url: '/pages/classify-list/classify-list',
			type: 'download-filled',
			name: '我的下载',
			count: '0'
		},
		{
			url: '/pages/classify-list/classify-list',
			type: 'star-filled',
			name: '我的评分',
			count: '2'
		},
		{
			url: '',
			type: 'chatboxes-filled',
			name: '联系客服',
			count: ''
		}
	])

	const otherItems = ref([{
			url: '/pages/notice/detail?id=653507c6466d417a3718e94b',
			type: 'notification-filled',
			name: '订阅更新',
		},
		{
			url: '/pages/notice/detail?id=6536358ce0ec19c8d67fbe82',
			type: 'flag-filled',
			name: '常见问题',
		}
	])

	const call = () => {
		uni.makePhoneCall({
			phoneNumber: '19112012580',

		})
	}

	const itemOnTap = (obj) => {
		let url;
		let type;
		switch (obj.name) {
			case '我的下载':
				type = 'download'
				break
			case '我的评分':
				type = 'score'
				break
			default:
				break
		}
		if (type != '') {
			url = obj.url + '?name=' + obj.name + '&type=' + type
		} else {
			url = obj.url
		}
		uni.navigateTo({
			url: url
		})
	}

	const getUserInfo = async () => {
		uni.showLoading({
			title: '加载中...'
		})
		let res = await apiUserInfo()
		uni.hideLoading()
		console.log(res)
		userinfo.value = res.data
		funcItems.value.forEach((e) => {
			if (e.name == '我的下载') {
				e.count = res.data.downloadSize
			}
			if (e.name == '我的评分') {
				e.count = res.data.scoreSize
			}
		})
	}
</script>

<style lang="scss" scoped>
	.user-layout {
		.user-info {
			display: flex;
			align-items: center;
			justify-content: center;
			flex-direction: column;
			padding: 50rpx 0;

			.avatar {
				width: 160rpx;
				height: 160rpx;
				border-radius: 50%;
				overflow: hidden;

				image {
					width: 100%;
					height: 100%;
				}
			}

			.ip {
				font-size: 44rpx;
				color: #333;
				padding: 20rpx 5rpx;
			}

			.address {
				font-size: 28rpx;
				color: #aaa;

			}

		}

		.section {
			width: 690rpx;
			margin: 50rpx auto;
			border: 1px solid #eee;
			border-radius: 10rpx;
			box-shadow: 0 0 30rpx rgba(0, 0, 0, 0.05);

			.list {
				.row {
					display: flex;
					justify-content: space-between;
					align-items: ce;
					padding: 0 30rpx;
					height: 100rpx;
					border-bottom: 1px solid #eee;
					position: relative;

					&:last-child {
						border-bottom: 0;
					}

					.left {
						display: flex;
						align-items: center;
						justify-content: center;

						.text {
							font-size: 22rpx;
							padding-left: 20rpx;
							color: #666;
						}
					}

					.right {
						display: flex;
						align-items: center;
						justify-content: center;

						.text {
							font-size: 22rpx;
							padding-left: 20rpx;
							color: #aaa;
						}
					}

					button {
						position: absolute;
						top: 0;
						left: 0;
						height: 100rpx;
						width: 100%;
						opacity: 0;
					}
				}
			}
		}
	}
</style>